package com.csweimei.rentmanagementtreasure.tenant.viewModel;

import android.app.Application;
import android.content.Intent;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableField;
import androidx.databinding.ObservableList;

import com.chad.library.BR;
import com.csweimei.rentmanagementtreasure.LoginActivity;
import com.csweimei.rentmanagementtreasure.R;
import com.csweimei.rentmanagementtreasure.base.AppConfig;
import com.csweimei.rentmanagementtreasure.http.Repository;
import com.csweimei.rentmanagementtreasure.http.entity.TabTwoScheduleListBean;
import com.csweimei.rentmanagementtreasure.pop.PopSchedule;
import com.csweimei.rentmanagementtreasure.pop.PopUtils;
import com.csweimei.rentmanagementtreasure.utils.ValidateUtils;

import java.util.Map;

import io.reactivex.observers.DisposableObserver;
import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.utils.RxUtils;
import me.goldze.mvvmhabit.utils.ToastUtils;
import me.tatarka.bindingcollectionadapter2.ItemBinding;

public class FTwoVM extends BaseViewModel<Repository> {
    public FTwoVM(@NonNull Application application, Repository model) {
        super(application, model);
    }

    public ObservableField<String> selDate = new ObservableField<>(ValidateUtils.getCurDate());

    @Override
    public void onResume() {
        super.onResume();
        getScheduleList();
    }

    public ObservableField<Integer> showNo = new ObservableField<>(View.VISIBLE);

    public ObservableList<TabTwoItemVM> observableList = new ObservableArrayList<>();
    public ItemBinding<TabTwoItemVM> itemBinding = ItemBinding.of(BR.viewModel, R.layout.item_schedule_two);

    private void getScheduleList() {
        Map<String,String> map = AppConfig.getCurMap(model);
        map.put("date",selDate.get());
        model.schedulelist(map)
                .compose(RxUtils.exceptionTransformer())
                .compose(RxUtils.schedulersTransformer())
                .doOnSubscribe(this)
                .subscribe(new DisposableObserver<TabTwoScheduleListBean>() {
                    @Override
                    public void onNext(TabTwoScheduleListBean o) {
                        observableList.clear();
                        if (o.issucc){
                            if (o.getData().size()>0){
                                showNo.set(View.GONE);
                                for (TabTwoScheduleListBean.DataDTO bean : o.getData()) {
                                    TabTwoItemVM tabTwoItemVM = new TabTwoItemVM(FTwoVM.this);
                                    tabTwoItemVM.setData(bean);
                                    observableList.add(tabTwoItemVM);
                                }
                            }else {
                                showNo.set(View.VISIBLE);
                            }

                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public BindingCommand scheduleAdd = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            if (model.isLogin()){
                PopSchedule popSchedule = new PopSchedule(mActivity);
                PopUtils.showPop(mActivity,popSchedule,false);
            }else {
                ToastUtils.showShort("请先登录");
                mActivity.startActivity(new Intent(mActivity, LoginActivity.class));
            }
        }
    });

}
